#include "../constant.h"

// 基于有头单链表的栈
typedef struct node
{
    DataType data;
    node *next;
} * Stack;

#include "stack.h"

Stack InitStack() // based on headed linklist
{
    Stack s = (node *)malloc(sizeof(node));
    s->next = NULL;
    return s;
}

DataType Push(Stack &s, DataType x) // head insert
{
    Stack item = (node *)malloc(sizeof(node));
    item->data = x;
    item->next = s;
    s = item;
    return x;
}

DataType Pop(Stack &s)
{
    if (IsEmpty(s))
    {
        warning("empty stack");
        return 0;
    }
    else
    {
        DataType front = s->data;
        node *tmp = s;
        s = s->next;
        free(tmp);
        return front;
    }
}

DataType Front(Stack s)
{
    if (IsEmpty(s))
    {
        warning("empty stack");
        return 0;
    }
    else
    {
        return s->next->data;
    }
}

int IsEmpty(Stack s)
{
    return s->next == NULL;
}

void printf(Stack s)
{
    node *p = s->next;
    while (p != NULL)
    {
        printf("%d ", p->data);
        p = p->next;
    }
    printf("\n⬆(top)\n");
}
